/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farent.solutions.plugins.stripes.hyperstripes.core;

import com.farent.solutions.plugins.stripes.hyperstripes.spi.WebLibrary;
import com.farent.solutions.plugins.stripes.hyperstripes.core.tags.StandardHTMLTagHandler;
import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;

/**
 *
 * @author user
 */
public class HyperStripesLibrary implements WebLibrary {

    @Override
    public String createHeaders(StripesApplicationContext applicationContext) {
        StringBuilder scriptBuilder = new StringBuilder();

        scriptBuilder.append(createStyles(applicationContext));

        scriptBuilder.append(createScripts(applicationContext));

        scriptBuilder.trimToSize();

        System.out.println(scriptBuilder.toString());
        return scriptBuilder.toString();
    }

    String createTagForHead(StripesApplicationContext applicationContext,
            List<Attribute> attributes, String tagName) {
        ComponentModel model = ComponentTagModelFactory.getInstance().
                getTagModelForTag(new QName(tagName), applicationContext);
        model.getAttributes().addAll(attributes);
        ComponentTagHandler tagHandler = new StandardHTMLTagHandler();

        return tagHandler.createContent(model, null);
    }

    String createScriptForHead(StripesApplicationContext applicationContext,
             String src) {
        StringBuilder scriptBuilder = new StringBuilder();

        scriptBuilder.append(applicationContext.getApplicationUrl());
        scriptBuilder.append(applicationContext.getResourceLocation(src));
        scriptBuilder.trimToSize();

        List<Attribute> attributes = new ArrayList<Attribute>();
        attributes.add(new Attribute("src", scriptBuilder.toString()));
        attributes.add(new Attribute("type", "text/javascript"));
        return createTagForHead(applicationContext, attributes, "script");
    }

    String createScripts(StripesApplicationContext applicationContext) {
        StringBuilder builder = new StringBuilder();
        for (JAVASCRIPT_FILES js_file : JAVASCRIPT_FILES.values()) {
            builder.append(createScriptForHead(applicationContext, js_file.filename));
        }
        builder.trimToSize();
        return builder.toString();
    }

    String createStylesForHead(StripesApplicationContext applicationContext, String src) {
        StringBuilder scriptBuilder = new StringBuilder();

        scriptBuilder.append(applicationContext.getApplicationUrl());
        scriptBuilder.append(applicationContext.getResourceLocation(src));

        scriptBuilder.trimToSize();

        ComponentModel scriptTagModel = ComponentTagModelFactory.getInstance().
                getTagModelForTag(new QName("link"), applicationContext);
        scriptTagModel.addAttribute(new Attribute("href", scriptBuilder.toString()));
        scriptTagModel.addAttribute(new Attribute("type", "text/css"));
        scriptTagModel.addAttribute(new Attribute("rel", "stylesheet"));
        scriptTagModel.addAttribute(new Attribute("media", "screen"));


        ComponentTagHandler tagHandler = new StandardHTMLTagHandler();

        return tagHandler.createContent(scriptTagModel, null);
    }

    String createStyles(StripesApplicationContext applicationContext) {
        StringBuilder builder = new StringBuilder();
        for (STYLESHEET_FILES css_file : STYLESHEET_FILES.values()) {
            builder.append(createStylesForHead(applicationContext, css_file.filename));
        }
        builder.trimToSize();
        return builder.toString();
    }

    public enum JAVASCRIPT_FILES {

        HS_JS("hs.js");
        final String filename;

        private JAVASCRIPT_FILES(String file) {
            filename = file;
        }
    }

    public enum STYLESHEET_FILES {

        RESET_CSS("reset.css"),
        NINE_SIXTY_CSS("960.css"),
        HS_CSS("hs.css");
        final String filename;

        private STYLESHEET_FILES(String file) {
            filename = file;
        }
    }
}
